package com.boomsecret.threadtransfer;

import java.util.concurrent.TimeUnit;

/**
 * @author wangjinliang on 2019/1/31.
 */
public class VolatileDemo {
    private static volatile boolean flag = true;

    public static void main(String[] args) throws Exception {
        ThreadA threadA = new ThreadA();
        threadA.start();

        TimeUnit.SECONDS.sleep(2);

        threadA.stopThread();
        threadA.join();
    }

    public static class ThreadA extends Thread {

        @Override
        public void run() {
            while (flag) {
                try {
                    System.out.println("...");
                    TimeUnit.MICROSECONDS.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

        public void stopThread() {
            flag = false;
        }
    }
}
